﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SpringStudio.Common;
using SpringStudio.DAL;

namespace Sdepci.InfoCode.BLL.Authority
{
    /// <summary>
    /// 用户类
    /// </summary>
    [Serializable]
    public class User
    {
        #region 属性定义

        private string _userId;
        /// <summary>
        /// 用户证档号
        /// </summary>
        public string UserId
        {
            get { return this._userId; }
        }

        private string _userName;
        /// <summary>
        /// 用户名称
        /// </summary>
        public string UserName
        {
            get { return this._userName; }
        }

        private string _userCode;
        /// <summary>
        /// 人员信息编码
        /// </summary>
        public string UserCode
        {
            get { return this._userCode; }
        }

        private string _deptInfo;
        /// <summary>
        /// 部门信息
        /// </summary>
        public string DeptInfo
        {
            get { return this._deptInfo; }
        }

        private string _password;
        /// <summary>
        /// 用户密码
        /// </summary>
        public string Password
        {
            get { return this._password; }
        }

        private string _roleGuid;
        /// <summary>
        /// 角色Guid
        /// </summary>
        public string RoleGuid
        {
            get { return this._roleGuid; }
        }

        #endregion

        #region 构造方法

        public User(string userId)
        {
            this._userId = userId;

            try
            {
                string sql = "Select UserName,UserCode,DeptInfo,Password,RoleGuid From InfoCodeUsers Where UserId=@UserId";
                DataSet ds = SqlHelper.ExecuteDataSet(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@UserId", this._userId));
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    this._userName = row["UserName"].ToString();
                    this._userCode = row["UserCode"].ToString();
                    this._deptInfo = row["DeptInfo"].ToString();
                    this._password = row["Password"].ToString();
                    this._roleGuid = row["RoleGuid"].ToString();
                }
                else
                {
                    throw new Exception("没有找到该值对应的详细记录！（参数：UserId='" + this._userId + "'）");
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
            }
        }

        public User(string userId, string userName, string userCode, string deptInfo, string password, string roleGuid)
        {
            this._userId = userId;
            this._userName = userName;
            this._userCode = userCode;
            this._deptInfo = deptInfo;
            this._password = password;
            this._roleGuid = roleGuid;
        }

        #endregion
    }
}
